<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * Copyright (c) 2010. Quantum Software Design LLC & Household Title & Escrow, LLC
 */

/**
 * Created by IntelliJ IDEA.
 * User: david.hoffer
 * Date: Jan 6, 2010
 * Time: 7:00:16 PM
 */

class MDRecordationAndTransferTaxCalculator
{
    private $rateInfoProperties;
    private $titleInsuranceCalculatorHelper;
    private $state;
    private $countyLocales;
    private $taxCalculatorHelper;
    private $feeCalculator;

    public function MDRecordationAndTransferTaxCalculator($state, $rateInfoProperties) 
    {
       $this->state = $state;
       $this->rateInfoProperties = $rateInfoProperties;
       
       $this->titleInsuranceCalculatorHelper = new TitleInsuranceCalculatorHelper();
       
       $this->taxCalculatorHelper = new TaxCalculatorHelper();
       
       $this->countyLocales = CountyLocale::getCountyLocales($state);
       $this->feeCalculator = new FeeCalculator($state, $rateInfoProperties);
    }    

    public function supportsCountyLocale() {
        return true;
    }

    public function getCountyLocales() {
        return $this->countyLocales;
    }

    public function supportsTownLocale() {
        return false;
    }

    public function getTownLocales($countyLocale) {
        return array();
    }

    public function calculatePurchaseRecordationAndTransferTax($countyLocale,$townLocale,$consideration,$loanAmounts,$qualifiedFirstTimeBuyer,$ownerOccupiedResidence,$requestedPayer) 
    {

        //echo "$countyLocale $consideration "; exit();
        //print_r($ownerOccupiedResidence); exit();
        $recordationTax = $this->getRecordationTax($countyLocale, $consideration, $ownerOccupiedResidence, true);
        //print_r($recordationTax); exit();        

        // 3/31/2011
        // In Maryland, can you program the system to address the following?
        //For purchase transactions, when the loan amount exceeds the sales price, the recordation tax is based on the loan amount, not the sales prices. The difference, however, is charged to the buyer.
        //For example, I am working on one transaction now.
        //The purchase price is $200,000, the loan amount is $250,000 â€“ the recordation tax in Prince Georgeâ€™s County is based on the $250,000, not $200,000â€¦.
        //Therefore, the total recordation tax is $1,250
        //However, the total recordation tax based on the purchase price is $1,000
        //Therefore, the difference is $250.00 â€“ all of that is charged to the buyer, regardless of the answer to the question of how transfer and recordation taxes are split.
        //
        //You can put the excess recordation tax on a separate line underneath the standard recordation tax if that is easier for you.
        $loanAmount = $this->taxCalculatorHelper->getLoanTotal($loanAmounts);
        //echo $loanAmount; exit();
        
        $extraBuyerRecordationTax = 0.0;
        if ($loanAmount > $consideration) {
            $loanAmountRecordationTax = $this->getRecordationTax($countyLocale, $loanAmount, $ownerOccupiedResidence, true);
            $extraBuyerRecordationTax = $loanAmountRecordationTax->getAmount() - $recordationTax->getAmount();
        }

        $propertyTransferTax = $this->getStatePropertyTransferTax($countyLocale, $consideration, $qualifiedFirstTimeBuyer);
        //print_r($propertyTransferTax); exit();
        
        $countyPropertyTransferTax = $this->getCountyTransferTax($countyLocale, $consideration, $qualifiedFirstTimeBuyer);
        //print_r($countyPropertyTransferTax); exit();

        // this email concerns how a buyer and seller (in Maryland) must split transfer taxes in one specific scenario:
        // buyer and seller may either split transfertaxes 50/50, buyer pays all, or seller pays all

        // however, when the buyer is a first time md homebuyer, the state transfer tax (normally a rate of .50 per $100)
        // is split in half (transaction is subject now to tax at .25 per $100 when the buyer is a first time MD homebuyer),
        // but the seller must pay that tax.

        // It is not permissible (in the case of a md first time homebuyer) for the buyer to pay the seller's tax even thought the tax is split in half.
        // when the buyer is not a first time md homebuyer, the buyer can pay the total state transfer tax. the logic in your programming should address this. let me know if you need me to send you a few exampled of the math and the breakdown of the taxes to be paid by buyer and seller in each of those scenarios.

//        double loanAmount = taxCalculatorHelper.getLoanTotal(loanAmounts);

        // zero indicates false
        $mortgage = $this->taxCalculatorHelper->getNumberOfLoans($loanAmounts);
        //echo $mortgage; exit();

        // CT-32 Added fees per State
        $fees = $this->feeCalculator->calculate(true, $mortgage, $ownerOccupiedResidence);        
        //print_r($fees); exit();

        if (!$this->getQualifiedFirstTimeBuyer($qualifiedFirstTimeBuyer)) {
            return RecordationAndTransferTaxCreator::createSplitRecordationAndTransferTaxes(
                    $this->state,
                    $propertyTransferTax, $countyPropertyTransferTax, $recordationTax,
                    $extraBuyerRecordationTax,
                    $fees,
                    $requestedPayer,
                    null);
        } else {
            return RecordationAndTransferTaxCreator::createSplitRecordationAndTransferTaxes(
                    $this->state,
                    $propertyTransferTax, $countyPropertyTransferTax, $recordationTax,
                    $extraBuyerRecordationTax,
                    $fees,
                    $requestedPayer,
                    EPayerSpecialCase::SellerMustPayStateTax);
        }
    }

    public function getHeaderHTML() {
        return "Maryland and the various counties in the state assess transfer and/or recordation taxes for both purchase and refinance transactions. For purchase transactions, state law may require that the taxes be split in a certain manner. The calculators factor in such requirements when determining the amount of taxes to be paid by the buyer and seller.";
    }

    public function getFooterHTML() {
        return null;
    }

    public function calculateRefinanceRecordationAndTransferTax($countyLocale, $loanAmounts, $ownerOccupiedResidence,$payoffAmounts,$sameLender) {

        //print_r($loanAmounts); exit();
        $loanAmount = $this->taxCalculatorHelper->getLoanTotal($loanAmounts);
        $payoffAmount = $this->taxCalculatorHelper->getPayoffTotal($payoffAmounts);

        // CT-41 We have to account for the fact that the tax is really paid on the difference between the the consideration and the payoff and in the
        // logic below we round up to the next half thousand each value, well that can be wrong and often is when the consideration is on an even half
        // thousand put the payoff is not.  See the tests for this issue, I'm not saying this is perfect logic but I couldn't find a failing test case.
        // todo: 03 17 2010 I changed the recordation tax calculation for purchases to just use a first cost tier of 0 cost, perhaps this approach could be used here too?
        $considerationHT = $this->titleInsuranceCalculatorHelper->roundUp2HalfThousands($loanAmount);
        $payoffHT = $this->titleInsuranceCalculatorHelper->roundUp2HalfThousands($payoffAmount);
        $diffHT = $this->titleInsuranceCalculatorHelper->roundUp2HalfThousands($loanAmount - $payoffAmount);
        if ($diffHT > ($considerationHT - $payoffHT)) {
            $payoffAmount -= 500.0;
        }

        // Tax on full amount
        $recordationTax = $this->getRecordationTax($countyLocale, $loanAmount, $ownerOccupiedResidence, false);

        // In MD, for refinance, if principal residence then they pay recordation tax on the difference between the new
        // loan amount and the payoff.
        if ($ownerOccupiedResidence != null && $ownerOccupiedResidence->getValue()) {

            // CT-5 They get the payoff amount savings on the cheaper dollars, that is, we calc tax on the full amount at
            // potentially a higher rate and then subtract off the savings at potentially a lower rate.
            $payoffRecordationTax = $this->getRecordationTax($countyLocale, $payoffAmount, $ownerOccupiedResidence, false);
            $tax = max($recordationTax->getAmount() - $payoffRecordationTax->getAmount(), 0.0);
            $recordationTax = new RecordationTax($recordationTax->getName(), $tax);
        }

        // In MD, for refinance, they only pay the recordation tax.
        return new RecordationAndTransferTaxes(
                null, null, $recordationTax,
                $this->feeCalculator->calculate(false, count($loanAmounts), $ownerOccupiedResidence),
                null);
    }

    protected function getRecordationTax($countyLocale, $consideration, $ownerOccupiedResidence, $purchase) {
        $halfThousands = $this->titleInsuranceCalculatorHelper->roundUp2HalfThousands($consideration);

        //echo $halfThousands; exit();
        $tierCosts = array();
        if ($countyLocale->equals(CountyLocale::Allegany)) {
            // Recordation Tax (State Documentary Stamps)	.................... $3.25 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.allegany.rate")/*3.25*/);
        } else if ($countyLocale->equals(CountyLocale::Anne_Arundel)) {
            // Recordation Tax (State Documentary Stamps)	.................... $3.50 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.anne_arundel.rate")/*3.50*/);
        } else if ($countyLocale->equals(CountyLocale::Baltimore_City)) {
            // In the city of baltimore, a partial exemption of recordation tas applies to the first 22,000  of the
            // consideration payable on the conveyance for an owener-occupied residence if the statement regarding
            // owever occupancy is submitted.
            // Ref: http___ernst.cc_subscribers_ResourceDownload.asp_SessionID={622E3C18-7028-4C50-995E-D4FCB3531467}&doc=repdfs_MD.pdf
            if ($purchase && $ownerOccupiedResidence != null && $ownerOccupiedResidence->getValue()) {
                $exemptionHT = $this->titleInsuranceCalculatorHelper->roundUp2HalfThousands($this->rateInfoProperties->getDoubleValue("recordation.md.baltimore_city.owneroccupied.tier")/*22000*/);
                $tierCosts[] = new TierCost((int) $exemptionHT, $this->rateInfoProperties->getDoubleValue("recordation.md.baltimore_city.owneroccupied.rate")/*0.0*/);
            }
            // Recordation Tax (State Documentary Stamps)	.................... $5.00 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.baltimore_city.rate")/*5.00*/);
        } else if ($countyLocale->equals(CountyLocale::Baltimore_County)) {
            // Recordation Tax (State Documentary Stamps)	.................... $2.50 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.baltimore_county.rate")/*2.50*/);
        } else if ($countyLocale->equals(CountyLocale::Calvert)) {
            // Recordation Tax (State Documentary Stamps)	.................... $5.00 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.calvert.rate")/*5.00*/);
        } else if ($countyLocale->equals(CountyLocale::Caroline)) {
            // Recordation Tax (State Documentary Stamps)	.................... $5.00 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.caroline.rate")/*5.00*/);
        } else if ($countyLocale->equals(CountyLocale::Carroll)) {
            // Recordation Tax (State Documentary Stamps)	.................... $5.00 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.carroll.rate")/*5.00*/);
        } else if ($countyLocale->equals(CountyLocale::Cecil)) {
            // Recordation Tax (State Documentary Stamps)	.................... $4.10 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.cecil.rate")/*4.10*/);
        } else if ($countyLocale->equals(CountyLocale::Charles)) {
            // Recordation Tax (State Documentary Stamps)	.................... $5.00 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.charles.rate")/*5.00*/);
        } else if ($countyLocale->equals(CountyLocale::Dorchester)) {
            // Recordation Tax (State Documentary Stamps)	.................... $5.00 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.dorchester.rate")/*5.00*/);
        } else if ($countyLocale->equals(CountyLocale::Frederick)) {
            // Recordation Tax (State Documentary Stamps)	.................... $6.00 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.frederick.rate")/*6.00*/);
        } else if ($countyLocale->equals(CountyLocale::Garrett)) {
            // Recordation Tax (State Documentary Stamps)	.................... $3.50 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.garrett.rate")/*3.50*/);
        } else if ($countyLocale->equals(CountyLocale::Harford)) {
            // Recordation Tax (State Documentary Stamps)	.................... $3.30 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.harford.rate")/*3.30*/);
        } else if ($countyLocale->equals(CountyLocale::Howard)) {
            // Recordation Tax (State Documentary Stamps)	.................... $2.50 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.howard.rate")/*2.50*/);
        } else if ($countyLocale->equals(CountyLocale::Kent)) {
            // Recordation Tax (State Documentary Stamps)	.................... $3.30 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.kent.rate")/*3.30*/);
        } else if ($countyLocale->equals(CountyLocale::Montgomery)) {
            // In montgomery municipality, a partial exemption of recordation tas applies to the first 50,000 of the consideration
            // payable on the conveyance for an ower-occupied residence if the statement regarding owner occupancy is submitted.
            // Please note that exemption is in addition to the sate transfer tax of 1/4% for first-time buyers of owner-occupied
            // residential property.
            // Ref: http___ernst.cc_subscribers_ResourceDownload.asp_SessionID={622E3C18-7028-4C50-995E-D4FCB3531467}&doc=repdfs_MD.pdf
            
            //print_r($ownerOccupiedResidence); exit();
            if ($purchase && $ownerOccupiedResidence != null && $ownerOccupiedResidence->getValue()) {  
                
                $exemptionHT = $this->titleInsuranceCalculatorHelper->roundUp2HalfThousands($this->rateInfoProperties->getDoubleValue("recordation.md.montgomery.0.tier")/*50000*/);
                $tierCosts[] = new TierCost((int) $exemptionHT, $this->rateInfoProperties->getDoubleValue("recordation.md.montgomery.0.rate")/*0.0*/);
            }
            // Recordation Tax (State Documentary Stamps)	.................... $3.45 per $500 or fraction thereof of consideration.
            // Recordation tax applies to deeds and to certain mortgages. Recordation tax on consideration or debt in
            // excess of $500,000 will be taxed at a rate of $5.00 per $500 or fraction thereof on the amount that
            // exceeds $500,000.
            $tierCosts[] = new TierCost($this->rateInfoProperties->getIntegerValue("recordation.md.montgomery.1.tier")/*1000*/, $this->rateInfoProperties->getDoubleValue("recordation.md.montgomery.1.rate")/*3.45*/);
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.montgomery.2.rate")/*5.00*/);
            //print_r($tierCosts); exit();
            
        } else if ($countyLocale->equals(CountyLocale::Prince_Georges)) {
            // Recordation Tax (State Documentary Stamps)	.................... $2.50 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.prince_georges.rate")/*2.50*/);
        } else if ($countyLocale->equals(CountyLocale::Queen_Annes)) {
            // Recordation Tax (State Documentary Stamps)	.................... $4.95 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.queen_annes.rate")/*4.95*/);
        } else if ($countyLocale->equals(CountyLocale::Somerset)) {
            // Recordation Tax (State Documentary Stamps)	.................... $3.30 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.somerset.rate")/*3.30*/);
        } else if ($countyLocale->equals(CountyLocale::St_Marys)) {
            // Recordation Tax (State Documentary Stamps)	.................... $4.00 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.st_marys.rate")/*4.00*/);
        } else if ($countyLocale->equals(CountyLocale::Talbot)) {
            // Recordation Tax (State Documentary Stamps)	.................... $3.30 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.talbot.rate")/*3.30*/);
        } else if ($countyLocale->equals(CountyLocale::Washington)) {
            // Recordation Tax (State Documentary Stamps)	.................... $3.80 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.washington.rate")/*3.80*/);
        } else if ($countyLocale->equals(CountyLocale::Wicomico)) {
            // Recordation Tax (State Documentary Stamps)	.................... $3.50 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.wicomico.rate")/*3.50*/);
        } else if ($countyLocale->equals(CountyLocale::Worcester)) {
            // Recordation Tax (State Documentary Stamps)	.................... $3.30 per $500 or fraction thereof of consideration
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.md.worcester.rate")/*3.30*/);
        } else {
            return null;
        }

        return new RecordationTax("Recordation Tax", $this->titleInsuranceCalculatorHelper->calculateTieredCost($halfThousands, $tierCosts, 0.0));
    }

    protected function getStatePropertyTransferTax($countyLocale, $consideration, $qualifiedFirstTimeBuyer) {

        $tax = null;

        if ($this->getQualifiedFirstTimeBuyer($qualifiedFirstTimeBuyer)) {
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.state.md.firsttimebuyer.rate");//0.0025;
        } else {
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.state.md.rate");//0.005;
        }

//        if (municipality == EMunicipality.MD_ALLEGANY) {
//            // Qualified first time Maryland buyers of owner-occupied residential property are eligible for 1/4% rate
//            // on State Transfer Tax; first $30,000 of consideration exempt from County Transfer Tax.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_BALTIMORE_COUNTY) {
//            // Qualified first time Maryland buyers of owner-occupied residential property are eligible for 1/4% rate
//            // on State Transfer Tax. Separate check for recordation and municipality taxes to Baltimore County.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_BALTIMORE_CITY) {
//            // Recordation tax applies to deeds and to certain mortgages. Buyers of principal residences are eligible
//            // for special tax rates included in web calculators. See state summary for details.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_ANNE_ARUNDEL) {
//            // State Property Transfer Tax	.................... $0.005 times consideration
//            // Qualified first time Maryland buyers of owner-occupied residential property are eligible for a 1/4% rate
//            // on the State Transfer Tax.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_CALVERT) {
//            // Qualified first time Maryland buyers of owner-occupied residential property are eligible for a 1/4% rate
//            // on the State Transfer Tax.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_CAROLINE) {
//            // Qualified first time Maryland buyers of owner-occupied residential property are eligible for a 1/4% rate
//            // on the State Transfer Tax.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_CARROLL) {
//            // Qualified first time Maryland buyers of owner-occupied residential property are eligible for a 1/4% rate
//            // on the State Transfer Tax.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_CECIL) {
//            // Qualified first time Maryland buyers of owner-occupied residential property are eligible for a 1/4% rate
//            // on the State Transfer Tax.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_CHARLES) {
//            // Qualified first time Maryland buyers of owner-occupied residential property are eligible for a 1/4% rate
//            // on the State Transfer Tax.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_DORCHESTER) {
//            // Qualified first time Maryland buyers of owner-occupied residential property are eligible for a 1/4% rate
//            // on the State Transfer Tax.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_FREDERICK) {
//            // Qualified first time Maryland buyers of owner-occupied residential property are eligible for a 1/4% rate
//            // on the State Transfer Tax.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_GARRETT) {
//            // Qualified first time buyers of owner-occupied residential property qualify for a 1/4% State Transfer Tax.
//            // The County Transfer Tax applies to all deeds that require State Transfer Tax, and the first $50,000 is exempt.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_HARFORD) {
//            // Recordation tax applies to deeds and to certain mortgages. Qualified first time Maryland buyers of
//            // owner-occupied residential property are eligible for 1/4% rate on State Transfer Tax; first $30,000 of
//            // consideration exempt from County Transfer Tax.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_HOWARD) {
//            // Recordation tax applies to deeds and to mortgages other than purchase-money mortages. Qualified first
//            // time Maryland buyers of owner-occupied residential property are eligible for a 1/4% rate on the
//            // State Transfer Tax.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_KENT) {
//            // Recordation tax applies to deeds and to certain mortgages. Qualified first time Maryland buyers of
//            // owner-occupied residential property qualify for a 1/4% State Transfer Tax. The County Transfer Tax
//            // applies to all deeds that require State Transfer Tax.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_PRINCE_GEORGE) {
//            // Recordation tax applies to deeds and to certain mortgages. Qualified first time Maryland buyers of
//            // owner-occupied residential property are eligible for 1/4% rate on State Transfer Tax. Make separate
//            // check for recordation and municipality tax to County.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_QUEEN_ANNE) {
//            // Recordation tax applies to deeds and to mortgages other than purchase-money mortgages. Qualified first
//            // time Maryland buyers of owner-occupied residential property are eligible for a 1/4% rate on the
//            // State and County Transfer Taxes.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_SOMERSET) {
//            // Recordation tax applies to deeds and to mortgages other than purchase-money mortages. Qualified first
//            // time Maryland buyers of owner-occupied residential property are eligible for a 1/4% rate on the State Transfer Tax.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_ST_MARY) {
//            // 	Recordation tax applies to deeds and to mortgages other than purchase-money mortages. Qualified first
//            // time Maryland buyers of owner-occupied residential property are eligible for a 1/4% rate on the State Transfer Tax.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_TALBOT) {
//            // 	Qualified first time buyers of owner-occupied residential property qualify for a State Transfer Tax
//            // rate of 1/4% and for a $50,000 exemption on the County Transfer Tax. Pay municipality tax to Clerk's Office.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_WASHINGTON) {
//            // Recordation tax applies to deeds and to certain mortgages. Qualified first time Maryland buyers of
//            // owner-occupied residential property are eligible for 1/4% rate on State Transfer Tax. First $50,000
//            // of consideration exempt from County Transfer Tax.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_WICOMICO) {
//            // Recordation tax applies to deeds and to mortgages other than purchase-money mortgages. Qualified first
//            // time Maryland buyers of owner-occupied residential property are eligible for a 1/4% rate on the State
//            // Transfer Tax.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else if (municipality == EMunicipality.MD_WORCESTER) {
//            // Qualified first time buyers of owner-occupied residential property qualify for a 1/4% State Transfer Tax.
//            // The County Transfer Tax applies to all deeds that require State Transfer Tax, and the first $50,000 is
//            // exempt. Pay municipality tax to Finance Office.
//            if (qualifiedFirstTimeBuyer) {
//                tax = consideration * 0.0025;
//            } else {
//                tax = consideration * 0.005;
//            }
//        } else {
//            tax = consideration * 0.005;
//        }

        return new StatePropertyTransferTax("State Transfer Tax", $tax);
    }

    private function getQualifiedFirstTimeBuyer($qualifiedFirstTimeBuyer) {
        return $qualifiedFirstTimeBuyer != null && $qualifiedFirstTimeBuyer->getValue();
    }

    protected function getCountyTransferTax($countyLocale, $consideration, $qualifiedFirstTimeBuyer) {

        $tax = null;

        if ($countyLocale->equals(CountyLocale::Allegany)) {
            // Qualified first time Maryland buyers of owner-occupied residential property are eligible for 1/4% rate
            // on State Transfer Tax; first $30,000 of consideration exempt from County Transfer Tax.
            if ($this->getQualifiedFirstTimeBuyer($qualifiedFirstTimeBuyer)) {
                $consideration = $consideration - 30000.00;
                $consideration = max($consideration, 0.0);
            }
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.allegany.rate");//0.005;
        } else if ($countyLocale->equals(CountyLocale::Anne_Arundel)) {
            // County Transfer Tax	.................... $0.01 times consideration
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.anne_arundel.rate");//0.01;
        } else if ($countyLocale->equals(CountyLocale::Baltimore_City)) {
            // City Transfer Tax	.................... $0.015 times consideration
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.baltimore_city.rate");//0.015;
        } else if ($countyLocale->equals(CountyLocale::Baltimore_County)) {
            // County Transfer Tax	.................... $0.015 times consideration
            // Qualified first time Maryland buyers of owner-occupied residential property are eligible for 1/4% rate
            // on State Transfer Tax. Separate check for recordation and municipality taxes to Baltimore County.
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.baltimore_county.rate");//0.015;
        } else if ($countyLocale->equals(CountyLocale::Calvert)) {
            // Has no entry for this
            $tax = $this->rateInfoProperties->getDoubleValue("recordation.county.md.calvert");//0.0;
        } else if ($countyLocale->equals(CountyLocale::Caroline)) {
            // Qualfied first time Maryland buyers of owner-occupied residential property are eligible for rate of 1/4%
            // on State Transfer Tax. First $75,000 of consideration exempt from County Transfer Tax.
            if ($this->getQualifiedFirstTimeBuyer($qualifiedFirstTimeBuyer)) {
                $consideration = $consideration - $this->rateInfoProperties->getDoubleValue("recordation.county.md.caroline.firsttimebuyer");//75000.00;
                $consideration = max($consideration, 0.0);
            }
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.caroline.rate");//0.005;
        } else if ($countyLocale->equals(CountyLocale::Carroll)) {
            // Has no entry for this
            $tax = $this->rateInfoProperties->getDoubleValue("recordation.county.md.carroll");//0.0;
        } else if ($countyLocale->equals(CountyLocale::Cecil)) {
            // Has no entry for this
            $tax = $this->rateInfoProperties->getDoubleValue("recordation.county.md.cecil");//0.0;
        } else if ($countyLocale->equals(CountyLocale::Dorchester)) {
            // Qualified first time Maryland buyers of owner-occupied residential property are eligible for 1/4% rate
            // on State Transfer Tax; first $30,000 of consideration exempt from County Transfer Tax.
            if ($this->getQualifiedFirstTimeBuyer($qualifiedFirstTimeBuyer)) {
                $consideration = $consideration - $this->rateInfoProperties->getDoubleValue("recordation.county.md.dorchester.firsttimebuyer");//30000.00;
                $consideration = max($consideration, 0.0);
            }
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.dorchester.rate");//0.005;
        } else if ($countyLocale->equals(CountyLocale::Frederick)) {
            // Has no entry for this
            $tax = $this->rateInfoProperties->getDoubleValue("recordation.county.md.frederick");//0.0;
        } else if ($countyLocale->equals(CountyLocale::Garrett)) {
            // Qualified first time buyers of owner-occupied residential property qualify for a 1/4% State Transfer Tax.
            // The County Transfer Tax applies to all deeds that require State Transfer Tax, and the first $50,000 is exempt.
            if ($this->getQualifiedFirstTimeBuyer($qualifiedFirstTimeBuyer)) {
                $consideration = $consideration - 50000.00;
                $consideration = max($consideration, 0.0);
            }
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.garrett.rate");//0.01;
        } else if ($countyLocale->equals(CountyLocale::Harford)) {
            // Recordation tax applies to deeds and to certain mortgages. Qualified first time Maryland buyers of
            // owner-occupied residential property are eligible for 1/4% rate on State Transfer Tax; first $30,000 of
            // consideration exempt from County Transfer Tax.
            if (getQualifiedFirstTimeBuyer($qualifiedFirstTimeBuyer)) {
                $consideration = $consideration - $this->rateInfoProperties->getDoubleValue("recordation.county.md.harford.firsttimebuyer");//30000.00;
                $consideration = max($consideration, 0.0);
            }
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.harford.rate");//0.01;
        } else if ($countyLocale->equals(CountyLocale::Howard)) {
            // 	Recordation tax applies to deeds and to mortgages other than purchase-money mortages. Qualified first
            // time Maryland buyers of owner-occupied residential property are eligible for a 1/4% rate on the State
            // Transfer Tax.
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.howard.rate");//0.01;
        } else if ($countyLocale->equals(CountyLocale::Kent)) {
            // Recordation tax applies to deeds and to certain mortgages. Qualified first time Maryland buyers of
            // owner-occupied residential property qualify for a 1/4% State Transfer Tax. The County Transfer Tax
            // applies to all deeds that require State Transfer Tax.
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.kent.rate");//0.005;
        } else if ($countyLocale->equals(CountyLocale::Montgomery)) {
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.montgomery.rate");//0.01;
        } else if ($countyLocale->equals(CountyLocale::Prince_Georges)) {
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.prince_georges.rate");//0.014;
        } else if ($countyLocale->equals(CountyLocale::Queen_Annes)) {
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.queen_annes.rate");//0.005;
        } else if ($countyLocale->equals(CountyLocale::Somerset)) {
            $tax = $this->rateInfoProperties->getDoubleValue("recordation.county.md.somerset");//0.0;
        } else if ($countyLocale->equals(CountyLocale::St_Marys)) {
            // Recordation tax applies to deeds and to mortgages other than purchase-money mortages. Qualified first
            // time Maryland buyers of owner-occupied residential property are eligible for a 1/4% rate on the State Transfer Tax.
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.st_marys.rate");//0.01;
        } else if ($countyLocale->equals(CountyLocale::Talbot)) {
            // Qualified first time buyers of owner-occupied residential property qualify for a State Transfer Tax
            // rate of 1/4% and for a $50,000 exemption on the County Transfer Tax. Pay municipality tax to Clerk's Office.
            if (getQualifiedFirstTimeBuyer($qualifiedFirstTimeBuyer)) {
                $consideration = $consideration - $this->rateInfoProperties->getDoubleValue("recordation.county.md.talbot.firsttimebuyer");//50000.00;
                $consideration = max($consideration, 0.0);
            }
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.talbot.rate");//0.01;
        } else if ($countyLocale->equals(CountyLocale::Washington)) {
            // Recordation tax applies to deeds and to certain mortgages. Qualified first time Maryland buyers of
            // owner-occupied residential property are eligible for 1/4% rate on State Transfer Tax. First $50,000
            // of consideration exempt from County Transfer Tax.
            if ($this->getQualifiedFirstTimeBuyer($qualifiedFirstTimeBuyer)) {
                $consideration = $consideration - $this->rateInfoProperties->getDoubleValue("recordation.county.md.washington.firsttimebuyer");//50000.00;
                $consideration = max($consideration, 0.0);
            }
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.washington.rate");//0.005;
        } else if ($countyLocale->equals(CountyLocale::Wicomico)) {
            $tax = $this->rateInfoProperties->getDoubleValue("recordation.county.md.wicomico");//0.0;
        } else if ($countyLocale->equals(CountyLocale::Worcester)) {
            // Qualified first time buyers of owner-occupied residential property qualify for a 1/4% State Transfer Tax.
            // The County Transfer Tax applies to all deeds that require State Transfer Tax, and the first $50,000 is
            // exempt. Pay municipality tax to Finance Office.
            if ($this->getQualifiedFirstTimeBuyer($qualifiedFirstTimeBuyer)) {
                $consideration = $consideration - $this->rateInfoProperties->getDoubleValue("recordation.county.md.worcester.firsttimebuyer");//50000.00;
                $consideration = max($consideration, 0.0);
            }
            $tax = $consideration * $this->rateInfoProperties->getDoubleValue("recordation.county.md.worcester.rate");//0.005;
        } else {
            //throw new IllegalArgumentException("Must specify a valid municipality");
            return null;
        }

        return new CountyPropertyTransferTax("County Transfer Tax", $tax);
    }
}

?>
